﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace DBTools.DBUpdate.Comm
{
    /// <summary>
    /// 
    /// </summary>
    public class Mssql
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mycommand"></param>
        /// <returns></returns>
        public static SqlDataReader QueryDataReader(string mycommand)
        {
            SqlDataReader mydr = null; 
            SqlConnection conn = new SqlConnection(Set.DbServerConnectionString);
            SqlCommand cmd = new SqlCommand(mycommand, conn);
            conn.Open();
            mydr = cmd.ExecuteReader();

            return mydr;
        }

         
        public static DataSet Query(string mycommand)
        {
            DataSet myds =null;
            try
            {
                myds = new DataSet(); 
                SqlConnection conn = new SqlConnection(Set.DbServerConnectionString);
                conn.Open();
                SqlDataAdapter myda = new SqlDataAdapter(mycommand, conn);
                myda.Fill(myds, "abc");
                conn.Close();
            }
            catch 
            {

            }
            return myds;
        }

   
        public static int ExecuteNonQuery(string mycommand)
        { 
            SqlConnection conn = new SqlConnection(Set.DbServerConnectionString);
            SqlCommand cmd = new SqlCommand(mycommand, conn);
            conn.Open();
           int i = cmd.ExecuteNonQuery();
            conn.Close();
            return i;
        } 
         
        public static int ExecuteNonQuery(List<string> mycommands)
        {
            int i = 0;
             
            SqlConnection ConnectionConfig = new SqlConnection(Set.DbServerConnectionString);
            ConnectionConfig.Open();
            SqlTransaction sqlTransaction = ConnectionConfig.BeginTransaction();
 
            string SqlOut = "";
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = ConnectionConfig;
                cmd.Transaction = sqlTransaction;
                foreach (string Sql in mycommands)
                {
                    if (Sql.Trim() == "")
                    {
                        continue;
                    }
                    SqlOut = Sql;
                    cmd.CommandText = Sql;
                    i = i + cmd.ExecuteNonQuery();
                }
                sqlTransaction.Commit();
            }
            catch (Exception Ex)
            {
                sqlTransaction.Rollback();
                return 0;
            }
            finally
            {
                ConnectionConfig.Close();
            }

            return i;
        } 
    } 
    public static class Ini
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private static String _IniFileName = "";

        public static String IniFileName
        {
            get { return Ini._IniFileName; }
            set { Ini._IniFileName = value; }
        }


         
        public static string GetIniSetStr(string section, string key, string def)
        {
            string ReturnStr = "";
            if (File.Exists(_IniFileName))
            {
                StringBuilder temp = new StringBuilder(255);

                int i = GetPrivateProfileString(section, key, def, temp, 255, _IniFileName);
                ReturnStr = temp.ToString().Trim();
            }
            return ReturnStr;

        }
         
        public static int GetIniSetInt(string section, string key, string def)
        {
            string ReturnStr = GetIniSetStr(section, key, def);
            try
            {
                return int.Parse(ReturnStr);
            }
            catch { }
            return 0;

        }

         
        public static void WriteIniSet(string section, string key, string val, string path)
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }
            long i = WritePrivateProfileString(section, key, val, path);
        }

         
        public static void WriteIniSet(string section, string key, string val)
        {
            WriteIniSet(section, key, val, _IniFileName);

        }
    }
 

}
